﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        string RP_Temple_Add(RP_Temple model, int ACID);


        [OperationContract]
        string RP_Temple_Update(RP_Temple model, int ACID);


        [OperationContract]
        string RP_TempleByID_Delete(Guid ID, int ACID);


        [OperationContract]
        RP_Temple RP_TempleByID_Get(Guid ID, int ACID);


        [OperationContract]
        List<RP_Temple> RP_Temple_Get(string sortBy, int ACID);


        [OperationContract]
        List<View_RP_Temple> GetRP_TempleBybilltypeid(int BillTypeID, int ACID);

        [OperationContract]
        string SetDefaultPrintTemple(Guid id, int ACID);

        [OperationContract]
        string AddRP_Templet(RP_Temple model, byte[] rp, int ACID);

        [OperationContract]
        string updateRP_Templet(RP_Temple model, byte[] rp, int ACID);

    }
    public partial class Service : IService
    {
        #region  添加RP_Temple表数据
        /// <summary>
        /// 添加RP_Temple
        /// </summary>
        /// <param name="model">RP_Temple实体</param>
        /// <returns>空或错误</returns>
        public string RP_Temple_Add(RP_Temple model, int ACID)
        {
            if (model.ID == null || model.ID.Equals(Guid.Empty))
            {
                return "『报表ID』不能为空！";
            }
            if (model.isDefault == null)
            {
                return "『缺省模版标志』不能为空！";
            }
            if (model.SysFlag == null)
            {
                return "『系统报表标志』不能为空！";
            }
            if (string.IsNullOrEmpty(model.billName))
            {
                return "『报表名称』不能为空！";
            }
            if (model.billtypeid == null)
            {
                return "『报表类型ID』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.RP_Temple.Count(p => p.ID == model.ID) > 0)
                    {
                        return "主键已存在！";
                    }
                    ctx.RP_Temple.InsertOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 添加失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  添加RP_Temple表数据事物用重构
        /// <summary>
        /// 添加RP_Temple事物用重构
        /// </summary>
        /// <param name="model">RP_Temple实体</param>
        /// <returns>空或错误</returns>
        private string RP_Temple_Add(RP_Temple model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (model.ID == null || model.ID.Equals(Guid.Empty))
            {
                return "『报表ID』不能为空！";
            }
            if (model.isDefault == null)
            {
                return "『缺省模版标志』不能为空！";
            }
            if (model.SysFlag == null)
            {
                return "『系统报表标志』不能为空！";
            }
            if (string.IsNullOrEmpty(model.billName))
            {
                return "『报表名称』不能为空！";
            }
            if (model.billtypeid == null)
            {
                return "『报表类型ID』不能为空！";
            }
            if (ctx.RP_Temple.Count(p => p.ID == model.ID) > 0)
            {
                return "主键已存在！";
            }
            ctx.RP_Temple.InsertOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  修改RP_Temple表数据
        /// <summary>
        /// 修改RP_Temple
        /// </summary>
        /// <param name="model">RP_Temple实体</param>
        /// <returns>空或错误</returns>
        public string RP_Temple_Update(RP_Temple model, int ACID)
        {
            if (model.ID == null || model.ID.Equals(Guid.Empty))
            {
                return "『报表ID』不能为空！";
            }
            if (model.isDefault == null)
            {
                return "『缺省模版标志』不能为空！";
            }
            if (model.SysFlag == null)
            {
                return "『系统报表标志』不能为空！";
            }
            if (string.IsNullOrEmpty(model.billName))
            {
                return "『报表名称』不能为空！";
            }
            if (model.billtypeid == null)
            {
                return "『报表类型ID』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.RP_Temple.Count(p => p.ID == model.ID) != 1)
                    {
                        return "未找到指定记录或记录不唯一！";
                    }
                    RP_Temple RP_Templemodel = ctx.RP_Temple.Single(p => p.ID == model.ID);
                    if (model.ID != null || model.ID.Equals(Guid.Empty))
                    {
                        RP_Templemodel.ID = model.ID;
                    }
                    if (model.billtypeid != null)
                    {
                        RP_Templemodel.billtypeid = model.billtypeid;
                    }
                    if (!string.IsNullOrEmpty(model.billName))
                    {
                        RP_Templemodel.billName = model.billName;
                    }
                    if (model.SysFlag != null)
                    {
                        RP_Templemodel.SysFlag = model.SysFlag;
                    }
                    if (model.isDefault != null)
                    {
                        RP_Templemodel.isDefault = model.isDefault;
                    }
                    if (model.TContent != null)
                    {
                        RP_Templemodel.TContent = model.TContent;
                    }
                    if (!string.IsNullOrEmpty(model.AssemblyName))
                    {
                        RP_Templemodel.AssemblyName = model.AssemblyName;
                    }
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 修改失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion



        #region  删除RP_Temple表ByID数据
        /// <summary>
        /// 删除RP_TempleByID
        /// </summary>
        /// <param name="ID">报表ID</param>
        /// <returns>空或错误</returns>
        public string RP_TempleByID_Delete(Guid ID, int ACID)
        {
            if (ID == null || ID.Equals(Guid.Empty))
            {
                return "『报表ID』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.RP_Temple.Count(p => p.ID == ID) != 1)
                    {
                        return null;
                    }
                    RP_Temple model = ctx.RP_Temple.Single(p => p.ID == ID);
                    ctx.RP_Temple.DeleteOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 删除失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  删除RP_Temple表ByID数据[事物用重构]
        /// <summary>
        /// 删除RP_TempleByID[事物用重构]
        /// </summary>
        /// <param name="ID">报表ID</param>
        /// <returns>空或错误</returns>
        private string RP_TempleByID_Delete(Guid ID, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (ID == null || ID.Equals(Guid.Empty))
            {
                return "『报表ID』不能为空！";
            }
            if (ctx.RP_Temple.Count(p => p.ID == ID) != 1)
            {
                return null;
            }
            RP_Temple model = ctx.RP_Temple.Single(p => p.ID == ID);
            ctx.RP_Temple.DeleteOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  获取RP_Temple表ByID数据
        /// <summary>
        /// 获取RP_TempleByID
        /// </summary>
        /// <param name="ID">报表ID</param>
        /// <returns>当前表</returns>
        public RP_Temple RP_TempleByID_Get(Guid ID, int ACID)
        {
            if (ID == null || ID.Equals(Guid.Empty))
            {
                return null;
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.RP_Temple.Count(p => p.ID == ID) != 1)
                    {
                        return null;
                    }
                    return ctx.RP_Temple.SingleOrDefault(p => p.ID == ID);
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion



        #region  获取RP_Temple表数据
        /// <summary>
        /// 获取RP_Temple
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<RP_Temple></returns>
        public List<RP_Temple> RP_Temple_Get(Expression<Func<RP_Temple, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.RP_Temple.ToList();
                        }
                        else
                        {
                            return ctx.RP_Temple.OrderBy(sortBy).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.RP_Temple.Where(predicate).ToList();
                        }
                        else
                        {
                            return ctx.RP_Temple.Where(predicate).OrderBy(sortBy).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取RP_Temple表数据
        /// <summary>
        /// 获取RP_Temple
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<RP_Temple></returns>
        public List<RP_Temple> RP_Temple_Get(string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    return RP_Temple_Get(null, sortBy, ACID);
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取RP_Temple表数据计数
        /// <summary>
        /// 获取RP_Temple表数据计数
        /// </summary>
        /// <returns>List<RP_Temple></returns>
        public int RP_Temple_GetCount(Expression<Func<RP_Temple, bool>> predicate, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        return ctx.RP_Temple.Count();

                    }
                    else
                    {
                        return ctx.RP_Temple.Where(predicate).Count();
                    }
                }
                catch
                {
                    return 0;
                }
            }
        }
        #endregion




        #region  获取RP_Temple分页表数据
        /// <summary>
        /// 获取RP_Temple分页表数据
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<RP_Temple></returns>
        public List<RP_Temple> RP_Temple_Getpage(int pageIndex, int pageSize, Expression<Func<RP_Temple, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.RP_Temple.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.RP_Temple.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.RP_Temple.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.RP_Temple.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  批量修改RP_Temple表数据
        /// <summary>
        /// 批量修改RP_Temple
        /// </summary>
        /// <param name="predicate">Expression<Func<RP_Temple, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<RP_Temple,RP_Temple>> 更新表达式树</param>
        /// <param name="returns">影响记录数</param>
        public int RP_Temple_batchUpdate(Expression<Func<RP_Temple, bool>> predicate, Expression<Func<RP_Temple, RP_Temple>> updater, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.RP_Temple.Update(predicate, updater);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量修改RP_Temple表数据事物用重构
        /// <summary>
        /// 批量修改RP_Temple事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<RP_Temple, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<RP_Temple,RP_Temple>> 更新表达式树</param>
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int RP_Temple_batchUpdate(Expression<Func<RP_Temple, bool>> predicate, Expression<Func<RP_Temple, RP_Temple>> updater, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.RP_Temple.Update(predicate, updater);
            return ok;
        }
        #endregion


        #region  批量删除RP_Temple表数据
        /// <summary>
        /// 批量删除RP_Temple
        /// </summary>
        /// <param name="predicate">Expression<Func<RP_Temple, bool>></param> 查找表达式树
        /// <param name="returns">影响记录数</param>
        public int RP_Temple_batchDel(Expression<Func<RP_Temple, bool>> predicate, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.RP_Temple.Delete(predicate);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量删除RP_Temple表数据事物用重构
        /// <summary>
        /// 批量删除RP_Temple事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<RP_Temple, bool>></param> 查找表达式树
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int RP_Temple_batchDel(Expression<Func<RP_Temple, bool>> predicate, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.RP_Temple.Delete(predicate);
            return ok;
        }
        #endregion

        public List<View_RP_Temple> GetRP_TempleBybilltypeid(int BillTypeID, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                return ctx.View_RP_Temple.Where(p => p.billtypeid == BillTypeID).ToList();
            }
        }

        public string SetDefaultPrintTemple(Guid id, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                string r = "";
                try
                {
                    RP_Temple rp1 = ctx.RP_Temple.Single(p => p.ID == id);
                    var rs = ctx.View_RP_Temple.Where(p => p.billtypeid == rp1.billtypeid);
                    foreach (var re in rs)
                    {
                        if (re.ID == id)
                        {
                            re.isDefault = true;
                        }
                        else
                        {
                            re.isDefault = false;
                        }
                        ctx.SubmitChanges();
                    }
                }
                catch (Exception ex)
                {
                    r = ex.Message;
                }
                return r;
            }
        }

        public string AddRP_Templet(RP_Temple model, byte[] rp, int ACID)
        {
            string result;
            bool isDefault = model.isDefault;
            Guid iD = model.ID;
            if (model.ID.Equals(Guid.Empty))
            {
                return "『模板ID』不能为空！";
            }
            bool sysFlag = model.SysFlag;
            if (string.IsNullOrEmpty(model.billName))
            {
                return "『模板名称』不能为空！";
            }
            int billtypeid = model.billtypeid;
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                if (ctx.RP_Temple.Count<RP_Temple>(p => (p.ID == model.ID)) > 0)
                {
                    return "模板ID已存在！";
                }
                if (ctx.RP_Temple.Count(p => ((p.billtypeid == model.billtypeid) && (p.billName == model.billName))) > 0)
                {
                    result = "已存在同名模板！";
                }
                else
                {
                    try
                    {
                        if (rp == null)
                        {
                            return "模板内容不能为空!";
                        }
                        if (rp.Length == 0)
                        {
                            return "模板内容不能为空!";
                        }
                        model.TContent = rp;
                        ctx.RP_Temple.InsertOnSubmit(model);
                        if (model.isDefault)
                        {
                            foreach (RP_Temple item in from p in ctx.RP_Temple where p.billtypeid == model.billtypeid select p)
                            {
                                if (item.ID == model.ID)
                                {
                                    item.isDefault = true;
                                }
                                else
                                {
                                    item.isDefault = false;
                                }
                            }
                        }
                        ctx.SubmitChanges();
                        result = string.Empty;
                    }
                    catch (Exception ex)
                    {
                        result = "添加数据失败！错误详细信息：" + ex;
                    }
                }
            }
            return result;
        }

        public string updateRP_Templet(RP_Temple model, byte[] rp, int ACID)
        {
            string result;
            bool isDefault = model.isDefault;
            int billtypeid = model.billtypeid;
            if (string.IsNullOrEmpty(model.billName))
            {
                return "『模板名称』不能为空！";
            }
            bool sysFlag = model.SysFlag;
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.RP_Temple.Count<RP_Temple>(p => (p.ID == model.ID)) == 1)
                    {
                        RP_Temple RP_Templemodel = ctx.RP_Temple.SingleOrDefault<RP_Temple>(p => p.ID == model.ID);
                        bool isdefault = RP_Templemodel.isDefault;
                        if (!string.IsNullOrEmpty(model.billName))
                        {
                            RP_Templemodel.billName = model.billName;
                        }
                        RP_Templemodel.SysFlag = model.SysFlag;
                        RP_Templemodel.isDefault = model.isDefault;
                        if (rp == null)
                        {
                            return "模板内容不能为空!";
                        }
                        if (rp.Length == 0)
                        {
                            return "模板内容不能为空!";
                        }
                        RP_Templemodel.TContent = rp;
                        if (!string.IsNullOrEmpty(model.AssemblyName))
                        {
                            RP_Templemodel.AssemblyName = model.AssemblyName;
                        }
                        IQueryable<RP_Temple> trpts = from p in ctx.RP_Temple
                                                      where p.billtypeid == model.billtypeid
                                                      select p;
                        if (model.isDefault)
                        {
                            foreach (RP_Temple item in trpts)
                            {
                                if (item.ID == model.ID)
                                {
                                    item.isDefault = true;
                                }
                                else
                                {
                                    item.isDefault = false;
                                }
                            }
                        }
                        else if (isdefault)
                        {
                            trpts.SingleOrDefault<RP_Temple>(p => p.SysFlag).isDefault = true;
                        }
                        ctx.SubmitChanges();
                        return string.Empty;
                    }
                    result = "未找到指定记录";
                }
                catch (Exception ex)
                {
                    result = "修改数据失败！错误详细信息：" + ex;
                }
                finally
                {
                    if (ctx != null)
                    {
                        ctx.Dispose();
                    }
                }
            }
            return result;
        }
    }
}

